package com.xywl.escort.view.bandcard

import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.BaseResp
import com.xywl.escort.bean.GetUserBankCardTokenReq
import com.xywl.escort.bean.MessageEvent
import com.xywl.escort.common.Constant
import com.xywl.escort.utils.RegexUtils
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_band_card_bind.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import timber.log.Timber
import javax.inject.Inject

@Route(path = "/xy/band/card/bind")
class BandCardBindActivity : BaseActivity(), BandCardBindContract.View {
    private val REQUEST_CODE_CHOOSE_BAND = 28
    @Inject
    lateinit var mPresenter: BandCardBindContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_band_card_bind)
        showBackButton()
        setTitleText(R.string.setting_add_band_card)

        mPresenter.takeView(this)
        addListener()

        EventBus.getDefault().register(this)
    }

    private fun addListener() {
        text_band_name.setOnClickListener {
            ARouter.getInstance()
                    .build("/xy/band/card/cooperative")
                    .navigation(this, REQUEST_CODE_CHOOSE_BAND)
        }
        btn_confirm.setOnClickListener {
            submit()
        }
    }

    private fun submit() {
        val req = GetUserBankCardTokenReq()
        val bandAccountName = edit_name.text.toString().trim()
        val idCard = edit_id_card.text.toString().trim()

        val bandCardNo = edit_band_card.text.toString().trim()
        val phoneNum = edit_phone.text.toString().trim()
        if (TextUtils.isEmpty(bandAccountName)) {
            ToastUtil.showTip(this, "请输入账户名")
            return
        }
        if (TextUtils.isEmpty(idCard)) {
            ToastUtil.showTip(this, "请输入身份证号码")
            return
        }
        if (TextUtils.isEmpty(bandCardNo)) {
            ToastUtil.showTip(this, "请输入银行卡号")
            return
        }
        if (!RegexUtils.checkMobile(phoneNum)) {
            ToastUtil.showTip(this, R.string.user_error_hint_phone)
            return
        }
        req.bankAccountName = bandAccountName
        req.certificatesType = "01"
        req.certificatesNumber = idCard
        req.bankCardNo = bandCardNo
        req.phoneNum = phoneNum
        mPresenter.getUserBankCardToken(req)
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun mainEventBus(event: MessageEvent) {
        Timber.tag("BandCardBindActivity").e("event.message=" + event.message)
        Timber.tag("BandCardBindActivity").e("event.type=" + event.type)
        when (event.type) {
            Constant.EventType.EVENT_ADD_BAND_CARD_TYPE -> {
                val message = event.message
                if (!TextUtils.isEmpty(message)) {
                    if (message == Constant.EventMessage.ADD_BAND_CARD_SUCCESS) {
                        finish()
                    }
                }
            }
            else -> {
            }
        }
    }

    override fun getUserBankCardTokenSuccess(resp: BaseResp?) {
        val bandAccountName = edit_name.text.toString().trim()
        val idCard = edit_id_card.text.toString().trim()
        val bandCardNo = edit_band_card.text.toString().trim()
        val phoneNum = edit_phone.text.toString().trim()
        ARouter.getInstance()
                .build("/xy/band/card/third")
                .withString("bandAccountName", bandAccountName)
                .withString("idCard", idCard)
                .withString("bandCardNo", bandCardNo)
                .withString("phone", phoneNum)
                .navigation()
    }

    override fun getUserBankCardTokenFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }

    override fun onDestroy() {
        super.onDestroy()

        EventBus.getDefault().unregister(this)
    }
}
